import { Module, VuexModule, Mutation, Action, getModule } from 'vuex-module-decorators';
import store from '@/store';
import { PENDING_PLUS, PENDING_MINUS, SET_DATA_TOKEN } from '@/store/mutation-types';

@Module({ name: 'global', store, dynamic: true })
class Global extends VuexModule {
  public language: string = 'zh_cn';
  public pendingRequests: number = 0;
  public _token: string = '';
  public data_token: string = '';

  @Action({ commit: PENDING_PLUS })
  public pendingPlus() {}

  @Action({ commit: PENDING_MINUS })
  public pendingMinus() {}

  @Action
  async setDataToken(str: string) {
    this[SET_DATA_TOKEN](str);
  }
  @Mutation
  public [PENDING_PLUS]() {
    this.pendingRequests = this.pendingRequests + 1;
  }

  @Mutation
  public [PENDING_MINUS]() {
    if (this.pendingRequests <= 0) {
      this.pendingRequests = 0;
    } else {
      this.pendingRequests = this.pendingRequests - 1;
    }
  }

  @Mutation
  public [SET_DATA_TOKEN](str: string) {
    this.data_token = str;
  }
}

export const GlobalModule = getModule(Global);
